package com.projx.accounting;

import com.projx.accounting.module.User;
import com.projx.accounting.service.UserService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class AccountingApplication {

	public static void main(String[] args) {
		SpringApplication.run(AccountingApplication.class, args);
	}

	@Bean
	CommandLineRunner ensureAdmin(UserService userService) {
		return args -> {
			String admin = "admin";
			String pwd = "secret123";
			if (userService.findByUsername(admin).isEmpty()) {
				User u = new User();
				u.setUsername(admin);
				u.setEmail("admin@example.com");
				u.setPassword(pwd);
				userService.create(u);
				System.out.println("[startup] created admin user (username=admin, password=secret123)");
			} else {
				System.out.println("[startup] admin user already exists");
			}
		};
	}

}
